package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.ZyCommunity;
import com.blb.community_admin.entity.ZyRoom;
import com.blb.community_admin.service.IZyRoomService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "房屋模块")
@RestController
@CrossOrigin
public class ZyRoomController {
    @Autowired
    private IZyRoomService zyRoomService;

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/zyroom/page")
    public ResponseResult<IPage<ZyRoom>> selectRoomPage(Long current) {
        System.out.println(current);
        return ResponseResult.ok(zyRoomService.selectRoomPage(current, 3L));
    }

    /**
     * @param current
     * @param roomStatus
     * @param size
     * @param communityName
     * @param unitName
     * @param buildingName
     * @return
     */
    @ApiOperation("模糊分页查询")
    @GetMapping("/zyroom/page/fuzzy")
    public ResponseResult<IPage<ZyRoom>> fuzzyPage(Long current, Long size, String roomStatus, String communityName, String unitName, String buildingName) {
        return ResponseResult.ok(zyRoomService.fuzzyPage(current, size, roomStatus, communityName, unitName, buildingName));
    }


    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("/zyroom")
    public ResponseResult<List<String>> getzyrooms() {
        List<ZyRoom> list = zyRoomService.list(null);
        ArrayList<String> statuslist = new ArrayList<>();
        for (ZyRoom zyRoom : list) {
            String s = zyRoom.getRoomStatus();
            statuslist.add(s);
        }


        return ResponseResult.ok(statuslist);
    }


    @Log(module = "房屋", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加")
    @PostMapping("/zyroom")
    public ResponseResult<String> addzyroom(@RequestBody ZyRoom zyRoom) {
        zyRoom.setCommunityId(1338423709557272577L);
        zyRoomService.save(zyRoom);
        return ResponseResult.ok("添加成功");
    }


    @Log(module = "房屋", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改")
    @PutMapping("/zyroom")
    public ResponseResult<String> update(@RequestBody ZyRoom zyRoom) {
        zyRoom.setCommunityId(1338423709557272577L);
        zyRoomService.updateById(zyRoom);
        return ResponseResult.ok("修改成功");
    }


    @Log(module = "房屋", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除")
    @DeleteMapping("/zyroom/{id}")
    public ResponseResult<String> delete(@PathVariable long id) {
        zyRoomService.removeById(id);
        return ResponseResult.ok("删除成功");
    }


    @Log(module = "房屋", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/zyroom/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<ZyRoom> ZyRooms) throws IOException {
        String path = EasyExcelUtil.write("房屋数据", ZyRoom.class, ZyRooms);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

}
